<?php

class stereogum_scraper extends BlogScraper
{
    function __construct()
    {
        parent::__construct('stereogum');
    }

    function getPosts()
    {
        return $this->xpath->query("//html//body//div[@class='about']//a");   
        
    }
    
    function getTitle($post)
    {
        $text = $this->xpath->query("//h1");$text = $post->getElementsByTagName('h3')->item(0)->getElementsByTagName('a')->item(0)->nodeValue;
        $text = $text->item(0)->getAttribute('href');
        return $text;
    }
    
    function getLink($post)
    {
        $text = $this->xpath->query("//h1/a");
        $text = $text->item(0)->getAttribute('href');
        return $text;
    }
    
    function getDate($post)
    {
        $text = $this->xpath->query("//div[@id='entry_date']");
        $date = date('Y-m-d', $text);
        return $date;
    }   
    
    function scrape()
    {
        $posts = self::getPosts();


        $scraped = 0;
        $this->post_index = 0;
               
        foreach ($posts as $post)
        {  
            if ($this->post_index >=3) exit;
            $post_exists = Database::query("SELECT * FROM `tracks` WHERE `source_url` LIKE '".$post->getAttribute("href")."'");
            
            if ($post_exists[0]['id']) 
            {
                echo "Found post: ".$post->getAttribute("href").' already exists';
                continue;
            }
            
            echo $post->getAttribute("href");
            $scraper = new stereogum_post_scraper($post->getAttribute("href"));
            if ($this->download) $scraper->download = true;
            $scraper->scrape();
            $this->post_index++;
        }
    }
   
}

class stereogum_post_scraper extends BlogScraper
{
    function __construct($url)
    {
        echo "cons".$url;
        parent::__construct('stereogum', $url);
        
    }
    
    function getPosts()
    {
        return $this->xpath->query("//html//body//div[@class='entry']");   
        
    }
 
    function getPostLinks()
    {
        return $this->xpath->query("//div[@class='entry']//a");   
        
    }
    
    function getTitle($post)
    {
        $text = $this->xpath->query("//div[@class='entry']//h1")->item(0)->nodeValue;
      
        return self::sanitize($text);
    }
    
    function getLink($post)
    {
        $text = $this->xpath->query("//a[@title='permalink']")->item(0);
        $text = $text->getAttribute('href');
        return $text;
    }
    
    function prepareTrack($track)
    {
        if ($track['url'][0] != "h" && $track['url'][0] != "w") $track['url'] = "http://stereogum.com".$track['url'];
        $track['name'] = str_replace('"', '', $track['name']);
        $track['song'] = str_replace('"', '', $track['song']);

        return $track;
    }
    
    function getDate($post)
    {
        $text = $this->xpath->query("//div[@id='entry_date']")->item(0);

        $date = date('Y-m-d', strtotime(str_replace(',', '', $text->nodeValue)));
        echo $date;
        return $date;
    }     
    
}

?>